import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

//引入login组件
import Login from "@@/login/login.vue";
import Home from "@@/home/home.vue";

import Welcome from '../views/welcome/welcome.vue'
import User from '../views/user/user.vue'

const router = new VueRouter({
  routes: [
    {
      path: "/login",
      name: "login",
      component: Login
    },
    {
      path: "/",
      name: "home",
      component: Home,
      redirect: {
        path: "welcome"
      },
      children: [
        {
          name: "welcome",
          path: "welcome",
          component: Welcome
        },
        {
          name: "user",
          path: "user",
          component: User
        },
      ]
    }
  ]
});

router.beforeEach((to,from,next)=>{
  //1 获取token 从localStorage
  // console.log(to);
  let isToken = window.localStorage.getItem("tokenLogin")
  if(isToken) {
    next()
  } else {
    if(to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})


export default router;
